<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <title>DDD framework generator</title>

  <!-- Bootstrap -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
  <style>
    select option {
      padding: 5px 5px;
      border-bottom: 1px solid #ddd;
    }
  </style>
</head>
<body>
  <div class="container">
    <h3>DDD代码生成器</h3>
    <div class="form-horizontal">
      <div class="form-group col-xs-6">
        <label for="groupId" class="col-sm-2 control-label">GroupId</label>
        <div class="col-sm-10">
          <input type="text" class="form-control" id="groupId" placeholder="groupId" value="com.heoller">
        </div>
      </div>
      <div class="form-group col-xs-6">
        <label for="artifactId" class="col-sm-2 control-label">ArtifactId</label>
        <div class="col-sm-10">
          <input type="text" class="form-control" id="artifactId" placeholder="artifactId" onblur="validArtifactId()">
        </div>
      </div>
    </div>

    <div class="form-horizontal">
      <div class="form-group col-xs-6" style="margin-left: 0">
        <button type="button" class="btn btn-primary" onclick="generate()">点击生成</button>
      </div>
    </div>
  </div>

  <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
  <script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js" integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ" crossorigin="anonymous"></script>
  <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
  <script type="application/javascript">
      function validArtifactId() {
          var artifactId = $("#artifactId").val();
          if (artifactId.indexOf("_") > 0) {
              alert("ArtifactId contains invalid character '_'")
              $("#artifactId").val(artifactId.replaceAll("_", "-"));
          }
      }

      function generate() {
          var tables = [];
          var $tables = $("#tables input");
          var length = $tables.length;
          for (var i=0; i < length; i++) {
              if ($tables.eq(i).is(":checked")) {
                  tables.push($tables.eq(i).val())
              }
          }

          var xhr = new XMLHttpRequest();
          xhr.open('POST', '/generate', true);
          xhr.responseType = 'blob';
          xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
          xhr.onload = function (e) {
            var blob = e.currentTarget.response;
            var contentDispo = e.currentTarget.getResponseHeader('Content-Disposition');
            var fileName = contentDispo.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/)[1];
            saveBlob(blob, fileName);
          };
          xhr.send(JSON.stringify({"groupId": $("#groupId").val(), "artifactId": $("#artifactId").val(),
            "namespaces": $("#namespaces").val(), "version": $("#version").val(),
            "tables": tables.join(","), "connParam": {"url": $("#url").val(), "database": $("#database").val(),
              "user": $("#user").val(), "password": $("#password").val()}}));
      }

      function saveBlob(blob, fileName) {
          var a = document.createElement('a');
          a.href = window.URL.createObjectURL(blob);
          a.download = fileName;
          a.dispatchEvent(new MouseEvent('click'));
      }
  </script>
</body>
</html>